exports.controllers={
	admin_market_categories: function(space) {
		if (!space.roles.marketconfig) return "-";
		var mode=space.fields.mode||"list";
		switch (mode) {
			case "list":
				space.items=space.mfact.list_hier("market_category",{order_by: "main.ordering"});
				return space.views.process("widgets/market_categories/admin_"+mode,space);
				break;
			case "edit":
				space.item={};
				var rule={order_by: "main.ordering"};
				if (space.fields.id) rule.id={type:"<>",value:space.fields.id};
				space.parents=space.mfact.list_hier("market_category",rule);
				if (space.fields.id) space.item=space.mfact.get("market_category",space.fields.id);
				space.datatypes=space.site.sql.execute_and_fetch("market_category_param_datatypes/list");
				space.param_rows=[];
				if (space.fields.id) {
					space.mfact.list("market_category_param_group",{order_by: "main.ordering",category_id:space.fields.id}).forEach(function(grp) {
						space.param_rows.push({
							rowClass: "group",
							id:"grp"+grp.id,
							level:0,
							cells:[grp.name]
						});
						space.mfact.list("market_category_param",{order_by: "main.ordering",param_group_id:grp.id}).forEach(function(param) {
							space.param_rows.push({
								rowClass: "param",
								id:param.id,
								level:1,
								cells:[param.name,param.suffix,param.show_short,param.show_short_search,param.show_full_search,param.datatype_id]
							});
						});
					});
				}
				return space.views.process("widgets/market_categories/admin_"+mode,space);
				break;
			case "save":
				var item;
				if (space.fields.id) {
					item=space.mfact.get("market_category",space.fields.id);
					item.UpdateFromObject(space.fields);
				} else {
					item=space.mfact.from_object("market_category",space.fields);
				}
				item.Save();
				var hashes=space.functions.parse_grids(space);
				var cgroups={};
				var cparams={};
				space.site.sql.execute_and_fetch("market_category_param_groups/list",{category_id:item.id}).forEach(function(x) {cgroups[x.id]=1;});
				space.site.sql.execute_and_fetch("market_category_params/list_of_category",{category_id:item.id}).forEach(function(x) {cparams[x.id]=1;});
				var mcpgs={};
				for (var param_id in hashes.params) {
					var hash=hashes.params[param_id];
					if (hash.rowClass!="group") continue;
					var group=space.mfact.from_object("market_category_param_group",hash);
					if (!(param_id.match(/new/))) group.id=param_id.match(/(\d+)/)[1];
					group.category_id=item.id;
					group.Save();
					mcpgs[param_id]=group.id;
					delete cgroups[param_id];
				}
				for (var param_id in hashes.params) {
					var hash=hashes.params[param_id];
					if (hash.rowClass!="param") continue;
					var param=space.mfact.from_object("market_category_param",hash);
					param.param_group_id=mcpgs[hash.parent_id];
					if (!(param_id.match(/new/))) param.id=param_id.match(/(\d+)/)[1];
					param.Save();
					delete cparams[param_id];
				}

				return JSON.stringify({redirect:{url: space.action}});
				break;
			case "delete":
				space.item=space.mfact.get("market_category",space.fields.id);
				return space.views.process("widgets/market_categories/admin_"+mode,space);
				break;
			case "delete_save":
				space.mfact.get("market_category",space.fields.id).Delete();
				return JSON.stringify({redirect:{url: space.action}});
				break;
			case "reorder":
				space.functions.reorder_grids(space,"_models/market_category/");
				return JSON.stringify({});
				break;
		} 
	}
};

